import modelExtend from 'dva-model-extend'
import { query,dele,modify,queryProductList,link} from './services/commodityBankService'
import { lwjPageModel } from 'utils/model'
import { routerRedux } from 'dva/router';
import { Modal, message } from 'antd'

export default modelExtend(lwjPageModel,{
  namespace:"commodityBank",
  state:{
    currentItem: {},
    selectRows: [],
    selectedRowKeys: [],
    pageOptions: {
      sort: 'createTime',
      order: 'desc'
    },
    queryParam: {},
    isExpand: false,
    modifyVisible:false,
    deleteVisible:false,
    currentType: "SALES",
    store: {
      modalVisible: false,
      selectedRows: [],
      selectedRowKeys: [],
      list: [],
      pagination: {
        //与lwjTable交互要用current
        current: 1,
        total: 0,
        showTotal: total => `共 ${total} 条`,
        showSizeChanger: true,
        showQuickJumper: true,
        defaultPageSize: 10
      },
      id: null,
      queryParam: {}
    } 
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === "/commodityBank/commodityBankList") {
          dispatch({ type: 'query', payload: {} })
        }
      })
    }

  },
  effects: {
    * modify({
      payload
    }, { select, call, put }) {
      yield call(modify, payload.storeId,payload.values);
      // yield put(
      // {
      //     type: 'setAuditVisible',
      //     payload: false
      //   }
      //)
      yield put(routerRedux.push('/success', { listUrl: 'commodityBank/commodityBankList', detailUrl: 'commodityBank/commodityBankList' }));
    },
    * dele({
      payload
    }, { select, call, put }) {
      yield call(dele, payload.storeId);
      // yield put(
      // {
      //     type: 'setAuditVisible',
      //     payload: false
      //   }
      //)
      yield put(routerRedux.push('/success', { listUrl: 'commodityBank/commodityBankList', detailUrl: 'commodityBank/commodityBankList' }));
    },
    * query({
      payload
    }, { select, call, put }) {
      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      });
      const pageOptions = yield select(({ commodityBank }) => commodityBank.pageOptions);
      const queryParam = yield select(({ commodityBank }) => commodityBank.queryParam);
      const queryPayload = {
        ...pageOptions,
        queryParam: {
          ...queryParam,

        }
      };
      const data = yield call(query, queryPayload);
      yield put({
        type: 'freshState',
        payload: {

        }
      })
      yield put({
        type: 'querySuccess',
        payload: {
          ...data
        }
      })
    },
    //请求异步数据
    * productList({ payload }, { select, call, put }) {
      const store = yield select(({ commodityBank }) => commodityBank.store);
      //调用异步service
      const data = yield call(queryProductList, {
        // 与service交互要用current,但是lwjTable返回的是start
        start: payload.start,
        limit: payload.limit,
        storeId: store.id,
        sort: 'id',
        order: 'DESC',
        ...store.queryParam
      });
      //调用同步事件
      yield put({
        type: 'productSuccess',
        payload: {
          list: data.content,
          pagination: {
            current: payload.start + 1,
            total: data.totalElements,
            showTotal: total => `共 ${total} 条`,
            showSizeChanger: true,
            showQuickJumper: true,
            defaultPageSize: data.size
          }
        }
      });
    },
    //关联&取关
    *link({ param, flag }, { select, call, put }) {
      yield call(link, param)
      message.success("操作成功");
      const store = yield select(({ commodityBank }) => commodityBank.store);
      const pagination = store.pagination;
      yield put({
        type: 'productList',
        payload: {
          start: pagination.current - 1,
          limit: pagination.defaultPageSize,
        }
      })
      if (flag) {
        yield put({
          type: 'setSelectedRows',
          selectedRows: [],
          selectedRowKeys: []
        })
      }
    },

  },
  reducers:{
    onSelectChange(state, { payload }) {
      return {
        ...state,
        ...payload
      }
    },
    expandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: true
      }
    },
    unExpandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: false
      }
    },
    setCurrentTab(state, { payload }) {
      return {
        ...state,
        currentType: payload
      }
    },
    freshState(state, { payload }) {
      return {
        ...state,
        selectRows: [],
        selectedRowKeys: []
      }
    },
    setCurrentItem(state, { payload }) {
      return {
        ...state,
        currentItem: payload
      }
    },
    setOptionType(state, { payload }) {
      return {
        ...state,
        optionType: payload
      }
    },
    setModifyVisible(state,{payload}){
      return{
        ...state,
        modifyVisible:payload

      }
    },
    setDeleteVisible(state,{payload}){
      return{
        ...state,
        deleteVisible:payload
      }
    },
    toggleModal(state, { flag, id }) {
      return {
        ...state,
        store: {
          modalVisible: flag,
          selectedRowKeys: [],
          selectedRows: [],
          list: [],
          id: id,
          pagination: {
            //与lwjTable交互要用current
            current: 1,
            total: 0,
            showTotal: total => `共 ${total} 条`,
            showSizeChanger: true,
            showQuickJumper: true,
            defaultPageSize: 10
          },
          queryParam: {}
        }
      }
    },
    productSuccess(state, { payload }) {
      let store = state.store;
      store.pagination = payload.pagination;
      store.list = payload.list;
      return {
        ...state,
        store
      }
    },
    setSelectedRows(state, { selectedRows, selectedRowKeys }) {
      let store = state.store;
      store.selectedRows = selectedRows;
      store.selectedRowKeys = selectedRowKeys;
      return {
        ...state,
        store
      }
    },
    onSelectChange(state, { selectedRowKeys, selectedRows }) {
      let store = state.store;
      store.selectedRowKeys = selectedRowKeys;
      store.selectedRows = selectedRows;
      return {
        ...state,
        store
      }
    },
    changeParam(state, { queryParam }) {
      let store = state.store;
      store.queryParam = queryParam;
      return {
        ...state,
        store
      }
    },
  }

})

